<?php

class Contacts extends Controller {

	function __construct()
	{
		parent::Controller();

		if (!$this->site_sentry->is_logged_in()) {redirect('login/');exit;}
		$this->load->model('contact_model', false, false, 'contacts');
	}

	function index()
	{
		


		$data['contactList'] = $this->contact_model->getContacts(); // activate the option
		$data['page_title'] = $this->lang->line('menu_root_system');
		$data['msg'] = '<p class="error">' . $this->session->flashdata('message') . "</p>\n";
		$data['extraHeadContent'] = "<script type=\"text/javascript\" src=\"". base_url()."js/newinvoice.js\"></script>\n";
		$data['extraHeadContent'] .= "<script type=\"text/javascript\" src=\"". base_url()."js/search.js\"></script>\n";
		$data['extraHeadContent'] .= "<link type=\"text/css\" rel=\"stylesheet\" href=\"". base_url()."css/invoice.css\" />\n";
		$offset = (int) $this->uri->segment(3, 0);
		$data['query'] = $this->contact_model->getContacts($offset, 5000);
		$data['total_rows'] = ($data['query']) ? $data['query']->num_rows() : 0;
		$data['page_title'] = $this->lang->line('menu_root_system');
		$data['msg'] = '<p class="error">' . $this->session->flashdata('message') . "</p>\n";
		$data['page_title'] = $this->lang->line('menu_root_system');
		$this->load->view('login_header', $data , false, 'common_assets');
		$this->load->view('index', false, false, 'contacts');
		$this->load->view('login_footer', false , false, 'common_assets');
	}

	function delete()
	{
		$id = (int) $this->uri->segment(3);
		$this->session->set_flashdata('deleteContact', $id);
		$contactInfo = $this->contact_model->getSingleContact($id);
		$data['deleteContact'] = $contactInfo->row();
		$data['page_title'] = $this->lang->line('menu_delete_contact');
				//	$this->load->view('login_header', $data , false, 'common_assets');
		$this->load->view('delete', $data, false, 'contacts');
		//$this->load->view('login_footer', false , false, 'common_assets');
	}

	function delete_confirmed()
	{
		$contact_id = $this->session->flashdata('deleteContact');
		$this->contact_model->deleteContact($vendor_id);
		$this->session->set_flashdata('deleteContact', $this->lang->line('contact_delete_success'));
		redirect('contacts/');
		exit;
	}

	function edit()
	{
		$id = (int) $this->uri->segment(3);
		$contactInfo = $this->contact_model->getSingleContact($id);		
		$data['row'] = $contactInfo->row();
		$data['extraHeadContent'] = "<link type=\"text/css\" rel=\"stylesheet\" href=\"".base_url()."css/form.css\" type=\"text/css\" \n />";
		//$data['extraHeadContent'] = "<link type=\"text/css\" rel=\"stylesheet\" href=\"".base_url()."css/theme.css\" type=\"text/css\" \n />";
		//$data['typeList'] = $this->parts_model->getPartType(); // activate the option
		//$data['uomList'] = $this->parts_model->getUOM(); // activate the option
		$data['page_title'] = $this->lang->line('menu_root_system');


			if ( $post = $this->input->post('contact_id') )
			{
				$data = array(
					'first_name'		=> $this->input->post('first_name'),
					'last_name'		=> $this->input->post('last_name'),
					'street1'		=> $this->input->post('street1'),
					'street2'		=> $this->input->post('street2'),
					'city'			=> $this->input->post('city'),
					'state'			=> $this->input->post('state'),
					'zipcode'		=> $this->input->post('zipcode'),
					'phone_work'		=> $this->input->post('phone_work'),
					'phone_mobile'		=> $this->input->post('phone_mobile'),
					'email'			=> $this->input->post('email'),
					'fax'			=> $this->input->post('fax'),

						);
					
				$this->db->where('contact_id', $this->input->post('contact_id'));
				$this->db->update('contact_info', $data);
				
				$this->session->set_flashdata('notification', 'Contact "'.$this->input->post("first_name").'"  "'.$this->input->post("last_name").'" has been updated ...');
				
				redirect('contacts/edit/'.$this->input->post('contact_id'));
				//redirect('contacts');
			}
			$this->load->view('login_header', $data , false, 'common_assets');
			$this->load->view('edit'.$this->input->post('contact_id'), false, false, 'contacts');
			$this->load->view('login_footer', false , false, 'common_assets');
		
	}

	function create()
	{


		if ( $post = $this->input->post('first_name') )
			{
				$data = array(
					'first_name'		=> $this->input->post('first_name'),
					'last_name'		=> $this->input->post('last_name'),
					'street1'		=> $this->input->post('street1'),
					'street2'		=> $this->input->post('street2'),
					'city'			=> $this->input->post('city'),
					'state'			=> $this->input->post('state'),
					'zipcode'		=> $this->input->post('zipcode'),
					'phone_work'		=> $this->input->post('phone_work'),
					'phone_mobile'		=> $this->input->post('phone_mobile'),
					'email'			=> $this->input->post('email'),
					'fax'			=> $this->input->post('fax'),

						);
						
				$this->contact_model->addContact($data);
				$id = $this->db->insert_id();	
					
				$this->session->set_flashdata('notification', 'Contact "'.$this->input->post("first_name").'"  "'.$this->input->post("last_name").'" has been created');	
				
				redirect('contacts/index');
			}
			else
			{
		$data['extraHeadContent'] = "<link type=\"text/css\" rel=\"stylesheet\" href=\"".base_url()."css/form.css\" type=\"text/css\" \n />";
		//$data['extraHeadContent'] = "<link type=\"text/css\" rel=\"stylesheet\" href=\"".base_url()."css/theme.css\" type=\"text/css\" \n />";
		//$data['typeList'] = $this->parts_model->getPartType(); // activate the option
		//$data['uomList'] = $this->parts_model->getUOM(); // activate the option
		$data['page_title'] = $this->lang->line('menu_root_system');
		$this->load->view('login_header', $data , false, 'common_assets');
		$this->load->view('new', false, false, 'contacts');
		$this->load->view('login_footer', false , false, 'common_assets');
			}
	}

	function show()
	{


		$id = (int) $this->uri->segment(3);
		$contactInfo = $this->contact_model->getSingleContact($id);		
		$data['row'] = $contactInfo->row();
		$data['extraHeadContent'] = "<link type=\"text/css\" rel=\"stylesheet\" href=\"".base_url()."css/form.css\" type=\"text/css\" \n />";
		//$data['extraHeadContent'] = "<link type=\"text/css\" rel=\"stylesheet\" href=\"".base_url()."css/theme.css\" type=\"text/css\" \n />";
		//$data['typeList'] = $this->parts_model->getPartType(); // activate the option
		//$data['uomList'] = $this->parts_model->getUOM(); // activate the option
		$data['page_title'] = $this->lang->line('menu_root_system');
		$this->load->view('login_header', $data , false, 'common_assets');
		$this->load->view('show', false, false, 'contacts');
		$this->load->view('login_footer', false , false, 'common_assets');
	}
}
?>
